//
//  MyChangePasswordVC.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/29.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "MyChangePasswordVC.h"
#import "SYRequestAPI+Wode.h"
#import "LoginViewController.h"
#import "AppDelegate.h"
@interface MyChangePasswordVC ()

@end

@implementation MyChangePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initSubView{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.navView];
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@50);
    }];
    [self.view addSubview:self.originalPasswordTF];
    [_originalPasswordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(@84);
        make.width.equalTo(@409);
        make.height.equalTo(@50);
    }];;
    [self.view addSubview:self.newPasswordTF];
    [_newPasswordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self->_originalPasswordTF.mas_bottom).offset(20);
        make.width.equalTo(@409);
        make.height.equalTo(@50);
    }];
    
    [self.view addSubview:self.confirmButton];
    [_confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self->_newPasswordTF.mas_bottom).offset(49);
        make.width.equalTo(@269);
        make.height.equalTo(@40);
    }];
}
- (WoDeNavView *)navView{
    if (!_navView) {
        WoDeNavView *topView = [[WoDeNavView alloc]init];
        topView.frame = CGRectMake(0, 0, self.view.frame.size.width,50);
        topView.titleLabel.text = @"修改密码";
        topView.editeButton.hidden = YES;
        _navView = topView;
    }
    return _navView;
}
- (UITextField *)originalPasswordTF{
    if (!_originalPasswordTF) {
        UITextField *tf = [[UITextField alloc]init];
        tf.placeholder = @" 请输入原密码";
        tf.layer.cornerRadius = 5;
        tf.layer.borderColor = LINCOLOR.CGColor;
        tf.layer.borderWidth = 1;
        tf.layer.masksToBounds = YES;
        _originalPasswordTF = tf;
    }
    return _originalPasswordTF;
}
- (UITextField *)newPasswordTF{
    if (!_newPasswordTF) {
        UITextField *tf = [[UITextField alloc]init];
        tf.placeholder = @" 请输入新密码";
        tf.layer.cornerRadius = 5;
        tf.layer.borderColor = LINCOLOR.CGColor;
        tf.layer.borderWidth = 1;
        tf.layer.masksToBounds = YES;
        _newPasswordTF = tf;
    }
    return _newPasswordTF;
}
- (UIButton *)confirmButton{
    if (!_confirmButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"确认修改" forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.layer.cornerRadius = 5;
        button.layer.masksToBounds = YES;
        [button setBackgroundImage:[UIImage imageWithColor:SELECTEDCOLOR forSize:CGSizeMake(200, 200)] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(confirm:) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton = button;
    }
    return _confirmButton;
}
- (void)confirm:(UIButton *)sender{
    [self chengePasswordRequest];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
#pragma mark - 修改密码
- (void)chengePasswordRequest{
    NSDictionary *parama;
    @try {
        parama = @{@"oldPassword":[NSString md5:_originalPasswordTF.text],
                   @"newPassword":[NSString md5:_newPasswordTF.text]};
    } @catch (NSException *exception) {
        [XSInfoView showInfo:@"请完善信息" onView:self];
    } @finally {
        
    }
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    [SYRequestAPI sy_getSafeServiceChangePassword:parama success:^(id  _Nonnull responseObject) {
        NSLog(@"修改密码 %@",responseObject);
        [MBProgressHUD mj_hideHUDForView:self.view];
        //重新登录
        [USERDEFAL removeObjectForKey:PassDefalut];
        [USERDEFAL removeObjectForKey:TokenDefalut];
        [USERDEFAL removeObjectForKey:RYUserPathDefalut];
        [USERDEFAL removeObjectForKey:RYUserNameDefalut];
        [USERDEFAL removeObjectForKey:RYUserIdDefalut];
        [USERDEFAL removeObjectForKey:RYTokenDefalut];
        [USERDEFAL removeObjectForKey:RYIMidIDDefalut];
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate.friendsArray removeAllObjects];
        [appDelegate.groupsArray removeAllObjects];
        [appDelegate.AllArray removeAllObjects];
        [[RCIMClient sharedRCIMClient] disconnect];

        [self.navigationController removeFromParentViewController];
        LoginViewController *login = [[LoginViewController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController = [[UINavigationController alloc]initWithRootViewController:login];
        [[UIApplication sharedApplication].keyWindow makeKeyAndVisible];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
    }];
}
@end
